;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Account.Receive", [])
        .controller("ReceiveCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            var btnPermissionCode = {
                receiveExport: window.ActionCode.receiveExport
            };
            var accOrgId = "";

            $scope.activeFlag = [true, false, false];
            $scope.totalInfo = {};
            $scope.btnPermission = {
                receiveExport: true
            };
            $scope.markInfo = {
                allCheckedOrder: false,
                allCheckedPrePay: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.prePayPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.tableConfig = {
                scrollWidth: 940,
                tableCol: [
                    {
                        name: "客户名称",
                        dataField: "accOrgName",
                        width: 300,
                        toSort: false,
                        isShow: true
                    },
                    {
                        name: "预收款金额",
                        dataField: "prepPayPrice",
                        width: 300,
                        toSort: false,
                        isShow: true,
                        type: "click",
                        clickFn: "viewPrepReceiverDetail"
                    },
                    {
                        name: "应收款金额",
                        dataField: "receiverPrice",
                        width: 300,
                        toSort: false,
                        isShow: true,
                        type: "click",
                        clickFn: "viewReceiverDetail"
                    }
                ]
            };
            $scope.prePayTableConfig = {
                scrollWidth: 930,
                tableCol: [
                    {
                        name: "客户名称",
                        dataField: "customerName",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "单据类型",
                        dataField: "billTypeStr",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "单据编号",
                        dataField: "billNo",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "交易金额",
                        dataField: "transactionAmount",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "原金额",
                        dataField: "originalAmount",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "交易后金额",
                        dataField: "totalAmount",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "创建时间",
                        dataField: "createDateStr",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "是否红冲",
                        dataField: "discardFlagStr",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "红冲时间",
                        dataField: "discardDateStr",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "备注",
                        dataField: "remark",
                        width: 155,
                        isShow: true
                    }
                ]
            };
            $scope.prePayInfo = {
                dataList: [],
                search: function (curPage) {
                    getPrePayList(curPage);
                }
            };

            $scope.dataList = [];
            $scope.toolbarConfig = {};
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/arap/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/arap/exportAll")
            };
            $scope.prePayExportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/prepPayDetail/export"),
                sectionParam: {ids: []},
                allUrl: ybHttp.RequestUrl("device", "/prepPayDetail/exportAll"),
                condition: {}
            };
            $scope.payExportParam = {
                allUrl: ybHttp.RequestUrl("device", "/arap/exportDetail"),
                condition: {
                    type: 0
                }
            };
            $scope.condition = {arapType: 0};

            $scope.changeTab = function (index) {
                angular.forEach($scope.activeFlag, function (activeFlag) {
                    activeFlag = false;
                });
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        getDataList();
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {arapType: 0};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                switch (type) {
                    case 'PrePay':
                        getPrePayIds();
                        break;
                    default:
                        getBillIds();
                        break;
                }
            };

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                switch (type) {
                    case 'PrePay':
                        getPrePayIds();
                        break;
                    default:
                        getBillIds();
                        break;
                }
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewPrepReceiverDetail":
                        viewPrepReceiverDetail(item);
                        break;
                    case "viewReceiverDetail":
                        viewReceiverDetail(item);
                        break;
                    default:
                        break;
                }
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            function getPrePayIds () {
                $scope.prePayExportParam.sectionParam.ids = [];
                angular.forEach($scope.prePayInfo.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.prePayExportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            function viewPrepReceiverDetail(item) {
                accOrgId = item.accOrgId;
                $scope.prePayExportParam.condition.customerId = accOrgId;
                getPrePayList(1);
                $scope.changeTab(2);
            }

            function getPrePayList (curPage) {
                if (curPage) $scope.prePayPageInfo.curPage = curPage;
                var url = '/prepPay/list';
                var filters = {
                    queryPageFlag: false,
                    customerId: accOrgId
                };
                var params = {
                    filters: filters,
                    pageSize: $scope.prePayPageInfo.pageSize,
                    pageNum: $scope.prePayPageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.isChecked = false;
                        item.billTypeStr = $filter('PrepPayBillType')(item.billType);
                        item.createDateStr = $filter("YbDate")(item.createDate, 'yyyy-MM-dd HH:mm:ss');
                        item.discardDateStr = $filter("YbDate")(item.discardDate, 'yyyy-MM-dd HH:mm:ss');
                        item.discardFlagStr = $filter('TrueOrFalse')(item.discardFlag, 'normal');
                    });
                    $scope.prePayPageInfo.totalRows = res.value.total;
                    $scope.prePayPageInfo.totalPages = res.value.pages;
                    $scope.prePayInfo.dataList = res.value.list;
                });
            }

            function viewReceiverDetail(item) {
                accOrgId = item.accOrgId;
                $scope.payExportParam.condition.orgId = accOrgId;
                getDetailDataList(1);
                $scope.changeTab(1);
            }

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;

                var service = "device",
                    reqUrl = "/arap/list",
                    param = {
                        pageSize: $scope.pageInfo.pageSize,
                        pageNum: $scope.pageInfo.curPage,
                        filters: angular.copy($scope.condition)
                    };

                if (!!curPage) {
                    param.pageNum = $scope.pageInfo.curPage = curPage;
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    if (resp.value.list.length > 0) {
                        var temp = resp.value.list[0];
                        $scope.totalInfo.totalPrepPayPrice = temp.totalPrepPayPrice;
                        $scope.totalInfo.totalReceiverPrice = temp.totalReceiverPrice;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            function initData() {
                $scope.markInfo.allCheckedOrder = false;
                $scope.markInfo.allCheckedPrePay = false;
                accOrgId = "";
                $scope.detailPage.condition = {};
                $scope.detailPage.dataList = [];
                $scope.prePayInfo.dataList = [];
                $scope.prePayPageInfo = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
            }

            /*********************** 详情页开始 ***********************/
            $scope.detailPage = {};
            $scope.detailPage.markInfo = {
                curRow: -1
            };
            $scope.detailPage.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.detailPage.condition = {};
            $scope.detailPage.dataList = [];
            $scope.detailPage.toolbarConfig = {};
            $scope.detailPage.tableConfig = {
                scrollWidth: 930,
                tableCol: [
                    {
                        name: "单据类型",
                        dataField: "billSingleF",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "单据编号",
                        dataField: "billNo",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "单据总金额",
                        dataField: "totalPrice",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "已收金额",
                        dataField: "receivePrice",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "剩余金额",
                        dataField: "remainPrice",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "单据创建时间",
                        dataField: "billDateF",
                        width: 155,
                        isShow: true
                    },
                    {
                        name: "开票状态",
                        dataField: "ticketStateShow",
                        width: 155,
                        isShow: true
                    }
                ]
            };

            $scope.detailPage.search = function (curpage) {
                getDetailDataList(curpage);
            };

            $scope.detailPage.reset = function () {
                $scope.detailPage.condition = {};
            };

            $scope.detailPage.switchToolbar = function (toolbar) {
                if (!!$scope.detailPage.toolbarConfig["show" + toolbar]) {
                    $scope.detailPage.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.detailPage.toolbarConfig["show" + toolbar] = true;
                });
            };

            $scope.detailPage.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.detailPage.selectRow = function (index) {
                $scope.detailPage.markInfo.curRow = index
            };


            function getDetailDataList(curPage) {
                $scope.detailPage.markInfo.curRow = -1;

                var service = "device",
                    reqUrl = "/arap/detailList",
                    param = {
                        pageSize: $scope.detailPage.pageInfo.pageSize,
                        pageNum: $scope.detailPage.pageInfo.curPage,
                        filters: angular.copy($scope.detailPage.condition)
                    };

                if (!!curPage) {
                    param.pageNum = $scope.detailPage.pageInfo.curPage = curPage;
                }

                param.filters.orgId = accOrgId;
                param.filters.type = 0;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var df = "yyyy-MM-dd HH:mm:ss";
                    angular.forEach(resp.value.list, function (item) {
                        item.billSingleF = $filter("orderType")(item.billSingle);
                        item.billDateF = $filter("YbDate")(item.billDate, df);
                        item.ticketStateShow = $filter("ProgressStatus")(item.ticketState, 'ticketStatus');
                    });
                    $scope.detailPage.pageInfo.totalRows = resp.value.total;
                    $scope.detailPage.pageInfo.totalPages = resp.value.pages;
                    $scope.detailPage.dataList = resp.value.list;
                });
            }

            /*********************** 详情页结束 ***********************/

            (function () {
                getDataList(1);
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);
